package com.example.zrq1010110087.graceplayer.main;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.example.zrq1010110087.graceplayer.data.MediaPlayerUser;
import com.example.zrq1010110087.graceplayer.data.Sdcard_MusicList;

import java.util.ArrayList;

/**
 * Created by zrq1010110087 on 2017/10/19.
 */

public class MusicService extends Service {
    @Nullable

    private MediaPlayerUser mediaPlayerUser;
    private  static final int MUSIC_STATUS_ERROR=-1;
    private static final  int MUSIC_STATUS_PLAY=0;
    private static final  int MUSIC_TATUS_PAUSE=1;
    private static final  int MUSIC_STATUS_RESUME=2;
    private static final  int MUSIC_STATUS_STOP=3;
//    private static final  int MUSIC_STATUS_COMPLETED=4;


    private static final  int MUSIC_COMMAND_PALY=0;
    private static final  int MUSIC_COMMAND_PAPUSE=1;
    private static final  int MUSIC_COMMAND_RESUME=2;
    private static final  int MUSIC_COMMAND_STOP=3;
    private static final  int MUSIC_COMMAND_PREVIOUS=4;
    private static final  int MUSIC_COMMAND_NEXT=5;
    private static final int MUSIC_COMMAND_ONCLICKITEM_NUMBER=6;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayerUser=new MediaPlayerUser(Sdcard_MusicList.getMusicArrayList(this),this);
    }

    @Override
    public void onDestroy() {
        mediaPlayerUser.releaseMediaPlayer();
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int status=intent.getIntExtra("status",0);
        switch (status) {


            case MUSIC_COMMAND_PALY: mediaPlayerUser.play_pause_resume();break;
            case MUSIC_COMMAND_STOP:mediaPlayerUser.stop(); break;
            case  MUSIC_COMMAND_PREVIOUS:mediaPlayerUser.moveToPrevious();break;
            case  MUSIC_COMMAND_NEXT:mediaPlayerUser.moveToNext();break;
            case MUSIC_COMMAND_ONCLICKITEM_NUMBER: int number=intent.getIntExtra("number",0);
                                                            mediaPlayerUser.onClickListItem(number);break;
        }
        Intent i=new Intent("android.intent.action.Music_StatusChangedReceiver");
        int musicStatus=mediaPlayerUser.getMusicStatus();
        i.putExtra("status",musicStatus);
        sendBroadcast(i);

            return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }




}
